<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function ajax(type, url) {

            let p = new Promise(function (resolve, reject) {

                var xhr = new XMLHttpRequest();
                xhr.open(type, url, true);
                xhr.send();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        // console.log( xhr.responseText )

                        resolve(xhr.responseText)
                        // reject()
                    }
                }

            })
            return p;

        }
        ajax("GET", "./1.json")

            .then(function (data) {
                // console.log(data);
                return ajax("GET", "./2.json")
            })

            .then(function (data) {
                // console.log(data);
            })

            .catch(function () {
                console.log("捕获 错误了")
            })
            .finally(function () {
                // console.log("不论成功失败都会执行")
            })
        let a = ajax("GET", "./1.json");
        let b = ajax("GET", "./2.json");
        // 合并：都对则对，有错都错
        Promise.all([a, b])
            .then(function (d) {
                console.log(d)
            })

        //竞争：那个先改变，使用哪一个
        Promise.race([a, b])
            .then(function (d) {
                console.log("竞争", d)
            })       
      var p2 = Promise.resolve(['aa', 'bb'])

        p2.then((data) => {
            console.log(data)
        })



        var p3 = Promise.reject(['dd', 'ee'])
        p3.then((data) => {
            console.log("成功", data)
        }, (data) => {
            console.log("失败", data)
        })

        p3.catch((data) => {
            console.log("失败", data)
        })         
    </script>
</body>

</html>